<?php

namespace app\v1\controller;

use app\model\NoteModel;
use support\Request;
use support\Response;
use Exception;

/**
 * 合作意向控制器
 * 
 * 提供用户提交合作意向的功能
 * 
 * @package app\v1\controller
 */
class NoteController
{
    /**
     * 提交合作意向
     * 
     * @param Request $request
     * @return Response
     */
    public function submit(Request $request): Response
    {
        try {
            // 参数验证
            $phone = $request->post('phone');
            if (empty($phone)) {
                throw new Exception('手机号不能为空');
            }

            // 验证手机号格式
            if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
                throw new Exception('请输入正确的手机号');
            }

            $content = $request->post('content');
            if (empty($content)) {
                throw new Exception('合作意向内容不能为空');
            }

            // 验证内容长度
            if (mb_strlen($content) < 10) {
                throw new Exception('合作意向内容至少需要10个字符');
            }

            if (mb_strlen($content) > 500) {
                throw new Exception('合作意向内容不能超过500个字符');
            }

            // 创建合作意向记录
            $note = new NoteModel();
            $note->phone = $phone;
            $note->content = $content;
            $note->status = 0; // 未处理状态
            $note->created_at = time();
            $note->updated_at = time();

            if (!$note->save()) {
                throw new Exception('提交合作意向失败');
            }

            return json([
                'data' => [
                    'id' => $note->id,
                    'phone' => $note->phone,
                    'content' => $note->content,
                    'created_at' => $note->created_at
                ],
                'msg' => '提交合作意向成功，我们会尽快与您联系',
                'code' => 1
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }
} 